package com.goodomen.garbage.web.web.controller.back;

import com.github.pagehelper.PageInfo;
import com.goodomen.garbage.common.persistence.model.Policy;
import com.goodomen.garbage.web.constant.ResponseCodeEnum;
import com.goodomen.garbage.web.dto.PolicyDto;
import com.goodomen.garbage.web.dto.response.PageResponseDto;
import com.goodomen.garbage.web.dto.response.ResponseDto;
import com.goodomen.garbage.web.service.PolicyService;
import com.goodomen.garbage.web.web.BackAuthentication;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台-相关法规政策维护
 *
 * @author : Zhao Da
 * @since : 2019/8/16 10:44
 */
@RestController
@RequestMapping(value = "/back/policy")
public class BackPolicyController {

    @Resource
    private PolicyService policyService;

    @ApiOperation(value = "（后台）法规政策查询", notes = "法规政策查询。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码（1,2,3...）", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页展示数", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "角色ID", required = false, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "roleName", value = "角色名", required = false, dataType = "string"),
    })
    @GetMapping("search")
    @BackAuthentication(code = "15")
    public PageResponseDto<Policy> search(
            HttpServletRequest request,
            @RequestParam("pageNum")int pageNum,
            @RequestParam("pageSize")int pageSize
    ) {
        String role = (String) request.getAttribute("role");
        PolicyDto policyDto = new PolicyDto();

        PageInfo<Policy> pageInfo = policyService.selectList(pageNum, pageSize, policyDto);
        return new PageResponseDto<>(pageInfo);
    }



    @ApiOperation(value = "（后台）法规政策查询", notes = "法规政策查询。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "角色id", required = true, dataType = "Long"),
    })
    @GetMapping("search/one")
    @BackAuthentication(code = "17")
    public ResponseDto<Policy> searchOne(HttpServletRequest request, Long id, HttpServletResponse response) {
        String role = (String) request.getAttribute("role");

        return new ResponseDto<>(ResponseCodeEnum.SUCCESS);
    }



    @ApiOperation(value = "（后台）法规政策新增", notes = "法规政策新增。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roleName", value = "角色名", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "remark", value = "角色描述", required = true, dataType = "string"),
    })
    @PostMapping("insert")
    @BackAuthentication(code = "34")
    public ResponseDto<Integer> insert(HttpServletRequest request) {
        String role = (String) request.getAttribute("role");
        PolicyDto policyDto = new PolicyDto();

        ResponseDto<Integer> result = new ResponseDto<>(ResponseCodeEnum.SUCCESS);

        Integer data = policyService.insert(policyDto);
        result.setData(data);
        return result;
    }



    @ApiOperation(value = "（后台）法规政策更新", notes = "法规政策更新。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "角色ID", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "roleName", value = "角色名", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "remark", value = "角色描述", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "permissions", value = "权限列表(英文逗号分隔）", required = false, dataType = "string"),
    })
    @PostMapping("update")
    @BackAuthentication(code = "16")
    public ResponseDto<Integer> update(
            HttpServletRequest request,
            PolicyDto policyDto
    ) {
        String role = (String) request.getAttribute("role");
        ResponseDto<Integer> result = new ResponseDto<>(ResponseCodeEnum.SUCCESS);
        Integer data = policyService.update(policyDto);
        result.setData(data);
        return result;
    }



    @ApiOperation(value = "（后台）法规政策删除", notes = "法规政策删除。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "角色ID", required = true, dataType = "Long"),
    })
    @PostMapping("delete")
    @BackAuthentication(code = "35")
    public ResponseDto<Integer> delete(HttpServletRequest request, Long id) {
        String role = (String) request.getAttribute("role");
        ResponseDto<Integer> result = new ResponseDto<>(ResponseCodeEnum.SUCCESS);

        Integer data = policyService.delete(id);
        result.setData(data);
        return result;
    }
}
